package com.zhongkun.datahome.service.export.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author huangjie
 * @since 2025/5/27
 */
public class QuantityConverter implements Converter<BigDecimal> {
    @Override
    public BigDecimal convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (cellData == null || cellData.getType() == CellDataTypeEnum.EMPTY) {
            return null;
        }

        if (cellData.getType() == CellDataTypeEnum.STRING) {
            String stringValue = cellData.getStringValue();
            if (StringUtils.isBlank(stringValue) || "/".equals(stringValue.trim()) ) {
                return null;
            }
        }

        return cellData.getNumberValue();
    }

    @Override
    public WriteCellData<?> convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        WriteCellData<Object> cellData = new WriteCellData<>(CellDataTypeEnum.NUMBER);
        cellData.setNumberValue(value);
        return cellData;
    }
}
